package com.xdtech.sys.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.fomat.TreeBuilder;
import com.xdtech.common.service.impl.BaseService;
import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.sys.dao.OperationDao;
import com.xdtech.sys.dao.PermissionDao;
import com.xdtech.sys.model.MenuFunction;
import com.xdtech.sys.service.MenuFunctionService;
import com.xdtech.sys.vo.MenuItem;
import com.xdtech.web.freemark.item.MenuButtonItem;
import com.xdtech.web.model.Menu;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;
@Service
public class MenuFunctionServiceImpl implements MenuFunctionService {
	@Autowired
	private PermissionDao permissionDao;
	@Autowired
	private OperationDao operationDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	public List<MenuFunction> loadMenuFunctionsByRoles(List<Long> roleIds) {
		if (EmptyUtil.isEmpty(roleIds)) {
			return null;
		}
		return permissionDao.getFunctionsByRoleIds(roleIds);
	}
	public List<MenuFunction> loadAllTreeFunctions() {
		List<MenuFunction> menus  = permissionDao.getAllFunctions();
		//格式菜单树
		return TreeBuilder.newTreeBuilder(MenuFunction.class, Long.class).buildToTreeList(menus);
	}
	
	public List<MenuFunction> loadAllFunctions() {
		List<MenuFunction> menus  = permissionDao.getAllFunctions();
		return menus;
	}
	
	public List<Menu> loadAllMenus() {
		List<MenuFunction> menuFunctions  = permissionDao.getAllFunctions();
		List<Menu> menus = new ArrayList<Menu>();
		Menu menu = null;
		for (MenuFunction menuFunction : menuFunctions) {
			menu = new Menu();
			menu.setId(menuFunction.getId());
			menu.setCode(menuFunction.getOperationCode());
			menu.setIcon(menuFunction.getIconName());
			menu.setName(menuFunction.getNameCN());
			if (menuFunction.getParent()!=null) {
				menu.setParent(new Menu(menuFunction.getParent().getId()));
			}
			menu.setUrl(menuFunction.getPageUrl());
			menus.add(menu);
		}
		
		
		return menus;
	}
	
	
	
	public List<MenuFunction> loadAllMenus(boolean needRoot) {
		// 加载所有菜单
		List<MenuFunction> allMenus =  permissionDao.getAllFunctions();
		if (needRoot) {
			MenuFunction mf = new MenuFunction();
			mf.setId(-1L);
			mf.setNameCN("功能菜单");
			allMenus.add(mf);
		}
		return allMenus;
	}
	
	public MenuItem loadItem(MenuItem item) {
		// 加载菜单信息
		MenuFunction mf = permissionDao.get(item.getId());
		BeanUtils.copyProperties(item, mf);
		return item;
	}
	
	public ResultMessage saveMenuItem(MenuItem item) {
		ResultMessage rm = new ResultMessage();
		// 保存菜单
		MenuFunction mf = new MenuFunction();
		BeanUtils.copyProperties(mf, item);
		if (null!=item.getpMenuId()&&item.getpMenuId()!=-1) {
			MenuFunction pMf = new MenuFunction();
			pMf.setId(item.getpMenuId());
			mf.setParent(pMf);
		}else {
			mf.setParent(null);
		}
		if (null==item.getId()) {
			//id 为空的话保存新记录
			mf.setId(null);
			permissionDao.save(mf);
		}else {
			//更新记录
			MenuFunction mmf = permissionDao.get(item.getId());
			BeanUtils.copyProperties(mmf, item);
			permissionDao.save(mmf);
		}
		
		return rm;
	}
	
	public List<MenuFunction> loadByRoleId(Long roleId) {
		return permissionDao.getMenuFunctionsByRoleId(roleId);
	}
	
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		// TODO Auto-generated method stub
		return null;
	}
	public void save(MenuFunction entity) {
		permissionDao.save(entity);
	}
	public void delete(MenuFunction entity) {
		permissionDao.delete(entity);
	}
	public void delete(Long id) {
		permissionDao.delete(id);
	}
	public MenuFunction get(Long id) {
		return permissionDao.get(id);
	}
	public List<MenuFunction> getAll() {
		return permissionDao.getAll();
	}
	public List<MenuButtonItem> loadAllMenuButtonItems() {
		List<MenuFunction> menuFunctions  = permissionDao.getAllFunctions();
		List<MenuButtonItem> menuButtonItems = new ArrayList<MenuButtonItem>();
		MenuButtonItem menuButtonItem = null;
		for (MenuFunction menuFunction : menuFunctions) {
			menuButtonItem = new MenuButtonItem();
			menuButtonItem.setId(menuFunction.getId());
			menuButtonItem.setCode(menuFunction.getOperationCode());
			menuButtonItem.setIcon(menuFunction.getIconName());
			menuButtonItem.setName(menuFunction.getNameCN());
			menuButtonItem.setIframe(menuFunction.getIframe());
			if (menuFunction.getParent()!=null) {
				menuButtonItem.setParent(new MenuButtonItem(menuFunction.getParent().getId()));
			}
			menuButtonItem.setUrl(menuFunction.getPageUrl());
			menuButtonItems.add(menuButtonItem);
		}
		return menuButtonItems;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2014-12-23下午10:26:52
	 * @modified by
	 * @param menuId
	 * @return
	 */
	public boolean deleteMenuInfo(Long menuId) {
		//删除关联权限sys_role_menu 表信息
		String sql = "DELETE FROM sys_role_menu WHERE menu_id=? OR menu_id IN (SELECT MENU_FUNCTION_ID FROM sys_menu_function mf WHERE mf.type=1 AND mf.PARENT_ID=?)";
		jdbcTemplate.update(sql,new Object[]{menuId,menuId});
		//先删除菜单下的按钮
		permissionDao.createSQLQuery("delete from sys_menu_function where oper_menu_id=? and type=1", menuId);
		//删除子菜单下的信息
		List<MenuFunction> subFunctions = permissionDao.findByHql("from MenuFunction mf where mf.parent.id=?", menuId);
		for (MenuFunction menuFunction : subFunctions) {
			deleteMenuInfo(menuFunction.getId());
		}
	    delete(menuId);		
		return true;
	}
	/* (非 Javadoc)
	 * loadMenusAndOperationWithRoleId
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param roleId
	 * @return
	 * @see com.xdtech.sys.service.MenuFunctionService#loadMenusAndOperationWithRoleId(java.lang.Long)
	 */
	public List<MenuItem> loadMenusAndOperationWithRoleId(Long roleId)
	{
		List<MenuItem> menuItems = new ArrayList<MenuItem>();
		MenuItem item = null;
		List<MenuFunction> menuFunctions = permissionDao.getAll();
		for (MenuFunction mf : menuFunctions)
		{
			item = new MenuItem();
			item.setId(mf.getId());
			item.setNameCN(mf.getNameCN());
			item.setOperationCode(mf.getOperationCode());
			if (null != mf.getParent())
			{
				item.setpMenuId(mf.getParent().getId());
				item.setpMenuName(mf.getParent().getNameEN());
			}
			menuItems.add(item);
		}
		return menuItems;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-2-3下午9:20:19
	 * @modified by
	 * @param b
	 * @return
	 */
	public List<MenuFunction> loadAllMenusAndOperations(boolean needRoot) {
		List<MenuFunction> allMenus = permissionDao.getAll();
		if (needRoot) {
			MenuFunction mf = new MenuFunction();
			mf.setId(-1L);
			mf.setNameCN("功能权限");
			allMenus.add(mf);
		}
		return allMenus;
		
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-2-15下午8:47:48
	 * @modified by
	 * @param entities
	 */
	@Override
	public void saveAll(List<MenuFunction> entities) {
		permissionDao.saveAll(entities);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:24:47
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
